const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    btime: util.ftime(new Date()),
    etime: util.ftime(new Date()),
    users: wx.getStorageSync('USERS'),
    date: util.fdate(new Date(),"YYYY-MM-DD")
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var interval = setInterval(function () {
      if (that.data.users.id) {
        clearInterval(interval);
        that.onUsersList();
      } else {
        that.setData({
          users: wx.getStorageSync('USERS')
        });
      }
    }, 1 * 500);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  bindDateChange: function (e) {//日期绑定
    this.setData({
      date: e.detail.value
    });
  },
  bindBTimeChange: function (e) {//日期绑定
    this.setData({
      btime: e.detail.value
    });
  },
  bindETimeChange: function (e) {//日期绑定
    this.setData({
      etime: e.detail.value
    });
  },
  onUsersList: function () {//获取成员
    var that = this;
    var parmas = {
      "uid": that.data.users.id
    }
    var isCache = false;
    var items = wx.getStorageSync(parmas.uid);
    if (items) {
      that.setData({
        items: items
      });
      isCache = true;
    }
    if (!isCache) {
      util.wxPost({
        url: "/rest/lufei/userList", params: parmas,
        success: function (res) {
          if (res.data.status == "000") {
            var items = res.data.data;
            var uid = that.data.users.id;
            for (var i in items) {
              if (items[i].id == uid) {
                items[i].checked = true;
              }
              if (items[i].realname == "--") {
                items[i].realname = items[i].nickname;
              }
            }
            wx.setStorage({
              key: parmas.uid,
              data: items,
            })
            that.setData({
              items: items
            });
          }
        }
      });
    }
  },
  formSubmit: function (e) {
    var that = this;
    var parmas = {
      "uid": that.data.users.id,
      "tid": that.data.users.teamId,
      "date": e.detail.value.date,
      "user": e.detail.value.user,
      "btime": e.detail.value.btime,
      "etime": e.detail.value.etime,
      "textDesc": e.detail.value.textDesc
    }
    if (parmas.uid && parmas.uid > 0 && parmas.textDesc && parmas.textDesc != "" && parmas.user.length > 0) {
      util.wxPost({
        url: "/rest/lufei/matter", params: parmas,
        success: function (res) {
          util.hideToast();
          if (res.data.status == "000") {
            wx.showToast({
              title: "保存成功",
              icon: "success",
              duration: 3000,
              complete: function () {
                wx.removeStorageSync(parmas.tid + ":week:" + parmas.date);
                wx.removeStorageSync(parmas.tid + ":home:" + parmas.date);
                wx.switchTab({
                  url: '/pages/index/index'
                });
              }
            });
          } else {
            wx.showToast({
              title: "保存失败!",
              icon: "none",
              duration: 1500
            });
          }
        }
      });
    } else {
      wx.showToast({
        title: "事项内容不能为空!",
        icon: "none",
        duration: 1500
      });
    }
  }
})